//
//  YKApproveListViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/9/24.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKApproveListViewController.h"
#import "YKClientListHeaderView.h"
#import "YKViewVisitLogItem.h"
#import "YKApproveListItem.h"
#import "YKApproveDetailViewController.h"
#import "YKClientOrderDetailViewController.h"
#import "YKDrugPriceDetailViewController.h"
#import "YKRequestPerformanceExchangeViewController.h"
#import "YKClientExchangeDetailViewController.h"
#import "YKProductInfoViewController.h"
#import "YKSelectView.h"
#import "YKApproveListIV2tem.h"
@interface YKApproveListViewController ()
@property (nonatomic, strong) NSString *businessTable;
@end

@implementation YKApproveListViewController



- (void)viewDidLoad {
    self.businessTable = @"";
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    @weakify(self)
    YKSelectView *selectView = [[YKSelectView alloc]initWithTitle:@"业务类型" row:@[@"全部类型",@"订单审批",@"商品价格",@"协议价格",@"客户兑换"] initialText:@"全部类型"];
    selectView.selectHandler = ^(id<YKDropdown> value) {
        //默认为全部(空字符串) 1 订单审批 2 协议价格审批 3 兑换审批 4 商品价格审批
        if ([value isEqual:@"全部类型"]) {
            self_weak_.businessTable = @"";
        }
        else if ([value isEqual:@"订单审批"]) {
            self_weak_.businessTable = @"1";
        }
        else if ([value isEqual:@"商品价格"]) {
            self_weak_.businessTable = @"4";
        }
        else if ([value isEqual:@"协议价格"]) {
            self_weak_.businessTable = @"2";
        }
        else if ([value isEqual:@"客户兑换"]) {
            self_weak_.businessTable = @"3";
        }
        [self_weak_ startLoadData];
    };
    [self.view addSubview:selectView];
    [selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];
    
//    YKClientListHeaderView* headerView = [[YKClientListHeaderView alloc]initWithTitles:@[@"类型",@"主题",@"创建人",@"提交时间"] weights:@[@1,@2,@1,@1]];
//    headerView.textFont = [UIFont systemFontOfSize:16];
//    [self.view addSubview:headerView];
//    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.equalTo(self.view);
//        make.top.equalTo(selectView.mas_bottom);
//    }];
//    headerView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
        make.leading.bottom.trailing.equalTo(self.view);
        make.top.equalTo(selectView.mas_bottom);
//        make.top.equalTo(headerView.mas_bottom);
    }];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableFooterView = [UIView new];
    self.dataSource.totalPageKey = @"page.totalpage";
    self.dataSource.currentPageKey = @"curpage";
    [self startLoadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self startLoadDataSilently];
}
- (NSDictionary *)pageViewControllerParameters {
    return @{
             kTFPageViewControllerItemClass: [YKApproveListIV2tem class],
             kTFPageViewControllerModelClass:[YKApproveListModel class],
             kTFPageViewControllerRequestURL: @"workExamine/searchCompletedMatter.htm",
             kTFPageViewControllerDataKeyPath:@"list",
             kTFPageViewControllerRequestParameter:@{@"c_name":self.c_name?:@"",@"task_state":self.task_state?:@"",@"businessTable":self.businessTable}
             };
}

- (void)didFinishLoad:(TFDataLoadPolicy)loadPolicy object:(id)object  sections:(NSArray<TFTableViewSection*>*)sections error:(NSError *)error {
    [super didFinishLoad:loadPolicy object:object sections:sections error:error];
    NSInteger count = [[object valueForKeyPath:@"page.count"] integerValue];
    if ([self.task_state isEqualToString:@"1"]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"numberUpdated" object:@(count)];
    }
    
}

- (void)customizeItem:(YKApproveListItem *)item {
    @weakify(self)
    item.hideSeparator = NO;
    item.separatorSunk = NO;
    item.selectionHandler = ^(__kindof YKApproveListItem *item, NSIndexPath *indexPath) {
        //MEMBER_ORDER_INFO 客户订单表
//        CUSTOMERDRUGPRICEMANAGER 客户药价信息表
//        POINTS_EXCHANGE 申请兑换信息申请表
//        SHOP_PRODUCTS 商品信息表
        if ([item.model.TASK_BNTABLE isEqualToString:@"MEMBER_ORDER_INFO"]) {
            //客户订单详情
            YKClientOrderDetailViewController *vc = [[YKClientOrderDetailViewController alloc]init];
            vc.ID = item.model.BUSINESSID;
            
            vc.hidesBottomBarWhenPushed = YES;
            vc.approveStatus = [self.task_state isEqualToString:@"1"] ? 1 : 2;
            [self_weak_.navigationController pushViewController:vc animated:YES];
        }
        else if ([item.model.TASK_BNTABLE isEqualToString:@"CUSTOMERDRUGPRICEMANAGER"]) {
            //客户药价详情
            YKDrugPriceDetailViewController *vc = [[YKDrugPriceDetailViewController alloc]init];
            vc.ID = item.model.BUSINESSID;
            vc.approveStatus = [self.task_state isEqualToString:@"1"] ? 1 : 2;
            vc.hidesBottomBarWhenPushed = YES;
            [self_weak_.navigationController pushViewController:vc animated:YES];
            
        }
        else if ([item.model.TASK_BNTABLE isEqualToString:@"POINTS_EXCHANGE"]) {
            //兑换
            if ([item.model.TYPENAME containsString:@"业绩积分"]) {
                YKRequestPerformanceExchangeViewController *vc = [[YKRequestPerformanceExchangeViewController alloc]init];
                vc.m_id = item.model.BUSINESSID;
                vc.approveStatus = [self.task_state isEqualToString:@"1"] ? 1 : 2;
                vc.hidesBottomBarWhenPushed = YES;
                [self_weak_.navigationController pushViewController:vc animated:YES];
            }
            else {
                YKClientExchangeDetailViewController *vc = [[YKClientExchangeDetailViewController alloc]init];
                vc.m_id = item.model.BUSINESSID;
                vc.approveStatus = [self.task_state isEqualToString:@"1"] ? 1 : 2;
                vc.hidesBottomBarWhenPushed = YES;
                [self_weak_.navigationController pushViewController:vc animated:YES];
            }
        }
        else if([item.model.TASK_BNTABLE isEqualToString:@"SHOP_PRODUCTS"]){
            //商品信息
            YKProductInfoViewController *vc = [[YKProductInfoViewController alloc]init];
            vc.businessId = item.model.BUSINESSID;
            vc.approveStatus = [self.task_state isEqualToString:@"1"] ? 1 : 2;
            vc.hidesBottomBarWhenPushed = YES;
            [self_weak_.navigationController pushViewController:vc animated:YES];
        }
    };
}

@end
